package com.yx.cloud.activity;


import android.view.View;
import android.widget.CheckBox;

import com.iot.product.sweeper.bean.AreaInfo;
import com.iot.product.sweeper.bean.CleanAreaInfo;
import com.iot.product.sweeper.bean.Path;
import com.iot.product.sweeper.bean.Robot;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.CleanParam;
import com.yx.cloud.R;
import com.yx.cloud.animator.AreaSetHelpDialog;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.AreaEditActivityBinding;
import com.yx.cloud.map.YXMapView;
import com.yx.cloud.map.layer.YXCleanAreaLayer;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.product.sweeper.bean.RobotStatus;
import com.iot.common.utils.Event;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

public class AreaEditActivity extends BaseActivity implements View.OnClickListener {
    private static final int MAX_COUNT = 10;
    private static String TAG = "AreaEditActivity";
    private YXMapView yxMapView;
    private int cleanTimes = 1;
    private AreaEditActivityBinding binding;

    @Override
    public void onCreate() {
        binding = AreaEditActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
//        Constants.deviceVariety = "map";
        yxMapView = findViewById(R.id.targetView);
        yxMapView.setBackgroundColor(getResources().getColor(R.color.background_color));
        Robot robot = (Robot) AppContext.get().getCurrentDevice();
        if (robot == null) {
            return;
        }
        JSONObject productConfig = AppContext.getConfig();
        yxMapView.initMap(robot.getMap());
        yxMapView.setLayerVisible(true, YXCleanAreaLayer.class);
        yxMapView.setMinCleanAreaSize((float) productConfig.optDouble(ProductConfigConstants.minCleanAreaSize));
        yxMapView.setCanRotateCleanArea(productConfig.optBoolean(ProductConfigConstants.canRotateCleanArea));

        binding.ivAreaReturn.setOnClickListener(this);
        binding.llAreaAdd.setOnClickListener(this);
        binding.cbAreaClean.setOnClickListener(this);
        binding.cbAreaCleanTimes.setOnClickListener(this);
        binding.ivAreaHelp.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        yxMapView.setCleanAreaEditable(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        yxMapView.setCleanAreaEditable(false);
    }

    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_area_return:
                Intent_utils.backIntent(this);
                break;
            case R.id.iv_area_help:
                AreaSetHelpDialog.newBuilder()
                        .context(this)
                        .viewId(R.layout.area_set_help_dialog)
                        .confirmCallback(new AreaSetHelpDialog.ConfirmCallback() {
                            @Override
                            public boolean onConfirm() {
                                return true;
                            }

                            @Override
                            public boolean onCancel() {
                                return true;
                            }
                        }).build().show();
                break;
            case R.id.ll_area_add:
                if (yxMapView.getCleanAreaLayers().size() >= MAX_COUNT) {
                    ToastUtil.showShort(AreaEditActivity.this, R.string.MM_T_AreaFull);
                } else {
                    yxMapView.addTempAreaLayer(YXCleanAreaLayer.class);
                }
                break;
            case R.id.cb_area_clean:
                CleanParam param = new CleanParam();
                List<YXCleanAreaLayer> layerList = yxMapView.getCleanAreaLayers();
                if (layerList.size() <= 0) {
                    ToastUtil.showShort(AreaEditActivity.this, R.string.MM_T_AddArea);
                    return;
                }
                List<CleanParam.SweepParam> paramList = new ArrayList<>();
                for (YXCleanAreaLayer cleanAreaLayer : layerList) {
                    CleanAreaInfo areaInfo = cleanAreaLayer.getAreaInfo();
                    areaInfo.setCleanTimes(cleanTimes);
                    CleanParam.AreaSweepParam param1 = new CleanParam.AreaSweepParam();
                    param1.setArea(areaInfo);
                    paramList.add(param1);
                }
                param.setType(RobotStatus.MODE_CUSTOM_CLEAN);
                param.setSweepParamList(paramList);
                AppContext.getProductService().startSweep(param);
                finish();
                break;
            case R.id.cb_area_clean_times:
                CheckBox checkBox = (CheckBox) v;
                if (checkBox.isChecked()) {
                    cleanTimes = 2;
                } else {
                    cleanTimes = 1;
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MSG_ROBOT_PATH:
                yxMapView.refreshDeviceLayer(AppContext.get().getCurrentDevice().getMap().getSweeper());
                yxMapView.refreshPathLayer((Path) event.getContent());
                break;
            case Event.MSG_ROBOT_MAP:
                yxMapView.initMap(AppContext.get().getCurrentDevice().getMap());
                break;
            case Event.MSG_ROBOT_AREA:
                yxMapView.refreshAreaLayer((List<AreaInfo>) event.getContent());
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent_utils.backIntent(this);
    }
}